//
//  FindRouteListViewController.m
//  SenMo
//
//  Created by ZengYuan on 15/10/12.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "FindRouteListViewController.h"
#import "ShowItemCell.h"
#import "ChannelListCell.h"

#import "InputDeviceViewController.h"
#import "QRCodeViewController.h"
#import "DeviceUnit.h"

@interface FindRouteListViewController ()

@end

@implementation FindRouteListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGRect rec = self.tableView.frame;
    rec.size.height = self.view.frame.size.height - NAVBARHEIGHT_STATUSBAR - 50;
    self.tableView.frame=rec;

    
    [self getData];
    
    self.titleLabel.text=@"搜索设备";
    
}

-(void)getData{
    
    Reachability*rea= [Reachability reachabilityForInternetConnection];
    //网络连接失败
    if (![rea isReachable]){
        
        [self doneLoadingTableViewData];
        if (fullloadingView) {
            [fullloadingView showErrorMsg:HTTPERROR_CANNOTREACH];
        }else{
            MBALERT(HTTPERROR_CANNOTREACH);
        }
        
        return;
    }
    freshPos=EGORefreshHeader;
    [self getListData];
}

-(void)getMoreData{
    Reachability*rea= [Reachability reachabilityForInternetConnection];
    if (![rea isReachable]){
        MBALERT(HTTPERROR_CANNOTREACH);
        return;
    }
    
    freshPos=EGORefreshFooter;
    
    [self getListData];
    NSLog(@"getMoreData");
}


-(void)getListData{
    
    NSString *url = [NSString stringWithFormat:MAINURL, @"search_ipc_in_router.action"];
    __unsafe_unretained typeof(self) weakSelf = self;
    
    NSString *sid = [[NSUserDefaults standardUserDefaults] objectForKey:@"session_id"];
    NSDictionary *dic = [[NSDictionary alloc] initWithObjects: @[sid,self.router_id] forKeys:@[@"session_id",@"router_id"]];
    NSString *json  =[dic JSONString];
    
    [AWHttpTool post:url body:json success:^(id json) {
        if (json) {
            [self doneLoadingTableViewData];
            //            [self dismissFullloadingView];
            NSInteger sta = [json[@"status"] integerValue];
            
            if (sta == 0) {
                
                NSMutableArray *tempArray = [DeviceUnit objectArrayWithKeyValuesArray:json[@"ipcs"]];
                //
                if (freshPos== EGORefreshHeader) {
                    self.dataArray = tempArray;
                    
                    //取到了班级数据
                    if (self.dataArray.count) {
                        [self dismissFullloadingView];
                        [self.tableView reloadData];
                    }else{
                        if (fullloadingView) {
                            [fullloadingView showErrorMsg:@"暂无设备"];
                        }else{
                            MBALERT(@"暂无设备");
                        }
                    }
                    
                    
                }else{
                }
                
            }else{
                NSString *err = json[@"msg"];
                MBALERT(err);
            }
        }
    } failure:^(NSError *error) {
        [self dismissFullloadingView];
        [self doneLoadingTableViewData];
        
        MBALERT(HTTPERROR_DATAFAIL);
    }];
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75;
}


- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
    
//    static NSString *CellId1 = @"CellId1";
//    MonitorListCell *cell=[_tableView dequeueReusableCellWithIdentifier:CellId1];
//    if (!cell) {
//        cell=[[[NSBundle mainBundle] loadNibNamed:@"MonitorListCell" owner:self options:nil] objectAtIndex:0];
//    }
//    
//    DeviceUnit *ipc = self.dataArray[indexPath.row];
//    cell.titleLabel.text = ipc.device_channel_name;
//    return cell;
//    
    
}

- (IBAction)enter:(id)sender {
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    InputDeviceViewController *irvc = [board instantiateViewControllerWithIdentifier:@"inputlvc"];
    [self.navigationController pushViewController:irvc animated:YES];
    
}

- (IBAction)scan:(id)sender {
    
    QRCodeViewController *qrvc = [[QRCodeViewController alloc]init];
    [self.navigationController pushViewController:qrvc animated:YES];
}
@end
